package com.lhd.mpaas.demo.base;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gyf.immersionbar.ImmersionBar;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by nuh on 2019/6/19
 * 基Fragment-懒加载
 */
public abstract class BaseLazyFragment extends Fragment {

    protected boolean isVisible;
    protected boolean isPrepared;
    protected boolean isFirst = true;

    private Unbinder unbinder;
    protected Activity mActivity;
    protected View mRootView;
    // 解决布局顶部和状态栏重叠问题 -- start
    protected View titleView;
    protected View statusBarView;
    protected int titleId;
    protected int statusBarId;
    // 解决布局顶部和状态栏重叠问题 -- end

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(getLayoutId(), container, false);
        } else {
            ViewGroup viewGroup = (ViewGroup) mRootView.getParent();
            if (viewGroup != null) {
                viewGroup.removeView(mRootView);
            }
        }
        titleView = mRootView.findViewById(titleId);
        statusBarView = mRootView.findViewById(statusBarId);
        return mRootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
        fitsLayoutOverlap();
        onInitView();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            lazyLoad();
        } else {
            isVisible = false;
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isPrepared = true;
        lazyLoad();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // 旋转屏幕为什么要重新设置布局与状态栏重叠呢？因为旋转屏幕有可能使状态栏高度不一样，如果你是使用的静态方法修复的，所以要重新调用修复
        fitsLayoutOverlap();
    }

    /**
     * 子类实现返回布局
     * 解决布局顶部和状态栏重叠问题的titleId或statusBarId在该方法初始化
     *
     * 子布局就不用重写 onCreateView 方法了
     *
     * @return layout
     */
    protected abstract int getLayoutId();

    protected void lazyLoad() {
        if (!isPrepared || !isVisible || !isFirst) {
            return;
        }
        onInitData();
        isFirst = false;
    }

    /**
     * InitView
     */
    protected void onInitView() {
    }

    /**
     * InitData
     */
    protected void onInitData() {
    }

    private void fitsLayoutOverlap() {
        if (statusBarView != null) {
            ImmersionBar.setStatusBarView(this, statusBarView);
        } else if (titleView != null) {
            ImmersionBar.setTitleBar(this, titleView);
        } else {
            ImmersionBar.setTitleBar(this, mRootView);
        }
    }
}
